package org.westos.ChinaSoft3;

import java.io.*;
import java.util.*;

public class Test {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要改变的薪资：");
        int i = scanner.nextInt();
        try {
            ReadFile readFile = new ReadFile();
            ArrayList<Employee> list = readFile.readFile();
            sort(list);
            changePrice(i, list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void changePrice(int SA, ArrayList<Employee> list) throws IOException {
        BufferedWriter out = null;
        try {
            Employee employee = list.get(list.size() - 1);
            employee.setSA(SA);
            System.out.println(employee);
            String str = employee.getId() + "," + employee.getName() + "," + employee.getAge() + "," + employee.getSex() + "," + employee.getSA();
            out = new BufferedWriter(new FileWriter("2.txt"));
            out.write(str);
            System.out.println("数据写入成功！");
        } finally {
            out.close();
        }
    }

    private static void sort(ArrayList<Employee> list) {
        TreeSet<Employee> empTreeSet = new TreeSet<>(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                int num = o1.getAge() - o2.getAge();
                int num2 = num == 0 ? o1.getName().length() - o2.getName().length() : num;
                int num3 = num2 == 0 ? o1.getName().compareTo(o2.getName()) : num2;
                return num3;
            }
        });
        for (Employee employee : list) {
            empTreeSet.add(employee);
        }
        System.out.println("排序后的结果为：" + empTreeSet);
    }
}
